package com.qqt.csr.archive.service;

import com.qqt.csr.archive.entity.reader.ArtificialMessage;
import com.qqt.csr.archive.mapper.ArtificialMessageMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yang hao
 * @Date: 2024/11/21 9:52
 * @Description: 人工客服 消息服务
 **/
@Slf4j
@Service
public class ArtificialMessageService {

    @Autowired
    private ArtificialMessageMapper artificialMessageMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据会话id获取人工客服消息
     * @param conversationId {@link String} 会话id
     * @return {@link List<ArtificialMessage>} 消息集
     */
    public List<ArtificialMessage> getByConversationId(String conversationId) {
        Query query = Query.query(Criteria.where("conversationId").is(conversationId))
                .with(Sort.by(Sort.Direction.ASC, "seq"));
        return mongoTemplate.find(query, ArtificialMessage.class);
    }

    /**
     * 获取会话中命中敏感词的消息数量
     * @param conversationId
     * @return
     */
    public Integer countSensitiveWordMsgCountBy(String conversationId){
        Query query = Query.query(
                Criteria.where("conversationId").is(conversationId)
                        .and("sensitiveWords").exists(true)
                        .not().size(0)
        );
        List<ArtificialMessage> messageList = mongoTemplate.find(query, ArtificialMessage.class);
        return (int) messageList.stream().map(ArtificialMessage::getSeq).distinct().count();
    }

}
